#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string>

// this method preserves the command line and restarts the process
//  in the background allowing the parent process to terminate
void restart(int argc, char* argv[])
{
  std::string args("sleep 5;");
  for(int i=0; i<argc; ++i)
  {
    args=args + " " + argv[i];
  }
  args = args + "&";

  for(int i=0; i<10; ++i)
  {
    printf("(%s:%d) running\n",__FILE__,__LINE__);
    ::usleep(1000000);
  }
 
  printf("(%s:%d) rebooting '%s'\n",__FILE__,__LINE__,args.c_str());
  system(args.c_str());
}

int main(int argc, char* argv[])
{
  printf("(%s:%d) main process initializing\n",__FILE__,__LINE__);
  restart(argc,argv);
  printf("(%s:%d) main process terminating\n",__FILE__,__LINE__);
}
